package com.nuanshui.heatedloan.credit.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.credit.model.record.HttpStatisticRecord;
import com.nuanshui.heatedloan.credit.mongodb.HttpStatisticRecordRepository;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageHandler;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by wangxw on 2017/7/22.
 */
@MqMessageHandler(RabbitMqQueue.RABBIT_MQ_HTTP_STATISTIC_RECORE)
public class HttpStatisticRecordHandler implements RabbitmqMessageHandler {

    private static Logger logger = LoggerFactory.getLogger("RiskConfirmOrderHandler");

    @Autowired
    private HttpStatisticRecordRepository httpStatisticRecordRepository;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try{
            logger.info("===================开始执行消息队列=============");
            String msg = new String(message.getBody());
            HttpStatisticRecord httpStatisticRecord = JSON.parseObject(msg,HttpStatisticRecord.class);
            httpStatisticRecordRepository.save(httpStatisticRecord);
            logger.info("===================执行消息队列完成=============");
        }catch( Exception e){
            e.printStackTrace();
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }

    }
}
